<!DOCTYPE html>


<html lang="zh-CN">


<head>
  <meta charset="utf-8" />
    
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    详解Python操作Excel神器openpyxl的各种操作.md |  
  </title>
  <meta name="generator" content="hexo-theme-ayer">
  
  <link rel="shortcut icon" href="/favicon.ico" />
  
  
<link rel="stylesheet" href="/dist/main.css">

  
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css">

  
<link rel="stylesheet" href="/css/custom.css">

  
  
<script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>

  
  

  

</head>

</html>

<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-oa/详解Python操作Excel神器openpyxl的各种操作"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  详解Python操作Excel神器openpyxl的各种操作.md
</h1>
 

    </header>
     
    <div class="article-meta">
      <a href="/2020/11/11/oa/%E8%AF%A6%E8%A7%A3Python%E6%93%8D%E4%BD%9CExcel%E7%A5%9E%E5%99%A8openpyxl%E7%9A%84%E5%90%84%E7%A7%8D%E6%93%8D%E4%BD%9C/" class="article-date">
  <time datetime="2020-11-10T16:00:00.000Z" itemprop="datePublished">2020-11-11</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/oa/">oa</a>
  </div>
  
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">1k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">4 分钟</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <h1 id="详解Python操作Excel神器openpyxl的各种操作"><a href="#详解Python操作Excel神器openpyxl的各种操作" class="headerlink" title="详解Python操作Excel神器openpyxl的各种操作"></a>详解Python操作Excel神器openpyxl的各种操作</h1><p>大家好，在之前的十几篇办公自动化系列文章中，我们大多是以<strong>真实的案例需求</strong>来讲解Python如何进行自动化办公操作，并且多次使用到<code>openpyxl</code>来处理表格，今天我们就来详细的<strong>盘点Python操作Excel神器openpyxl的各种操作！</strong></p>
<blockquote>
<p>本文将以详细图表/代码的形式讲解如何对Excel进行<strong>读取</strong>、<strong>写入</strong>及<strong>样式调整</strong>，可以当成<strong>速查手册</strong>使用，随用随查，建议收藏！</p>
</blockquote>
<h2 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h2><p><code>openpyxl</code>是一个非标准库，因此需要自行安装，安装过程并不困难，<strong>Windows/Mac</strong>用户均可以在命令行(CMD)/终端(Terminal)中使用pip安装</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pip install openpyxl</span><br></pre></td></tr></table></figure>

<h2 id="前置知识"><a href="#前置知识" class="headerlink" title="前置知识"></a>前置知识</h2><p>在使用这个模块之前我们需要对Excel表格结构有个比较细致的了解，见下图：<img src="http://iubest.gitee.io/pic/640-1601168112420.png" alt="img">简而言之，一个Excel工作簿<code>workbook</code>由一个或者多个工作表<code>sheet</code>组成，一个sheet可以看作是多个行<code>row</code>组成，也可以看作是多个列<code>column</code>组成，而每一行每一列都由多个单元格<code>cell</code>组成！</p>
<h2 id="读取Excel"><a href="#读取Excel" class="headerlink" title="读取Excel"></a>读取Excel</h2><h3 id="1-载入Excel"><a href="#1-载入Excel" class="headerlink" title="1.载入Excel"></a>1.载入Excel</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">from openpyxl import load_workbook</span><br><span class="line">workbook &#x3D; load_workbook(filename&#x3D;&#39;测试.xlsx’) </span><br><span class="line">print(workbook.sheetnames)</span><br></pre></td></tr></table></figure>

<p>注意<code>load_workbook</code>只能打开已经存在的Excel，不能创建新的工作簿</p>
<h3 id="2-根据名称获取工作表"><a href="#2-根据名称获取工作表" class="headerlink" title="2.根据名称获取工作表"></a>2.根据名称获取工作表</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">from openpyxl import load_workbook</span><br><span class="line">workbook &#x3D; load_workbook(filename&#x3D;&#39;其他.xlsx&#39;) </span><br><span class="line">print(workbook.sheetnames)</span><br><span class="line">sheet &#x3D; workbook[&#39;工作业务&#39;]</span><br></pre></td></tr></table></figure>

<p>如果只有一张工作表也可以用：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sheet &#x3D; workbook.active</span><br></pre></td></tr></table></figure>

<h3 id="3-获取表格内容所在的范围"><a href="#3-获取表格内容所在的范围" class="headerlink" title="3.获取表格内容所在的范围"></a>3.获取表格内容所在的范围</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">print(sheet.dimensions)</span><br></pre></td></tr></table></figure>

<h3 id="4-获取某个单元格的具体内容"><a href="#4-获取某个单元格的具体内容" class="headerlink" title="4.获取某个单元格的具体内容"></a>4.获取某个单元格的具体内容</h3><p>这边提供两种方法，注意都需要以<code>cell.value</code>形式输出具体值<img src="http://iubest.gitee.io/pic/640-1601168149291.png" alt="img"></p>
<h3 id="5-获取某个单元格的行、列、坐标"><a href="#5-获取某个单元格的行、列、坐标" class="headerlink" title="5.获取某个单元格的行、列、坐标"></a>5.获取某个单元格的行、列、坐标</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">print(cell.row, cell.column, cell.coordinate)</span><br></pre></td></tr></table></figure>

<h3 id="6-获取多个格子的值"><a href="#6-获取多个格子的值" class="headerlink" title="6.获取多个格子的值"></a>6.获取多个格子的值</h3><p><img src="http://iubest.gitee.io/pic/640-1601168112421.webp" alt="img">这里也有一个细节，Excel中每一列由字母确定，是字符型；每一行由一个数字确定，是<strong>整型</strong>。当然，上面的三种方法都是获取一堆表格，现在要输出每一个表格的值就需要<strong>遍历</strong>：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">for cell in cells: </span><br><span class="line">    print(cell.value)</span><br></pre></td></tr></table></figure>

<p>三种方法依然有自己的<strong>局限性</strong>，如果我需要特定范围的值，且懒得换算成字母数字坐标。例如我想要获取第2行至第5行、第1列至第3列的全部单元格。因此必须掌握第4种方法：</p>
<p><img src="http://iubest.gitee.io/pic/640-1601168112426.png" alt="img"></p>
<h3 id="7-读取所有的行"><a href="#7-读取所有的行" class="headerlink" title="7.读取所有的行"></a>7.读取所有的行</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">for row in sheet.rows: </span><br><span class="line">    print(row)</span><br></pre></td></tr></table></figure>

<h2 id="Excel写入"><a href="#Excel写入" class="headerlink" title="Excel写入"></a>Excel写入</h2><h3 id="1-保存Excel"><a href="#1-保存Excel" class="headerlink" title="1. 保存Excel"></a>1. 保存Excel</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">workbook.save(filename&#x3D;&#39;Excel工作表1.xlsx&#39;)</span><br></pre></td></tr></table></figure>

<ul>
<li>如果读取和写入Excel的路径相同则为对原文件进行<strong>修改</strong>，</li>
<li>如果读取和写入Excel的路径不同则为<strong>保存成新的文件</strong></li>
</ul>
<h3 id="2-写入单元格"><a href="#2-写入单元格" class="headerlink" title="2.写入单元格"></a>2.写入单元格</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">cell &#x3D; sheet[&#39;A1&#39;] </span><br><span class="line">cell.value &#x3D; &#39;业务需求</span><br></pre></td></tr></table></figure>

<h3 id="3-写入一行或多行数据"><a href="#3-写入一行或多行数据" class="headerlink" title="3.写入一行或多行数据"></a>3.写入一行或多行数据</h3><p><img src="http://iubest.gitee.io/pic/640-1601168112419.png" alt="img"><img src="http://iubest.gitee.io/pic/640-1601168112424.png" alt="img"></p>
<h3 id="4-将公式写入单元格并保存"><a href="#4-将公式写入单元格并保存" class="headerlink" title="4.将公式写入单元格并保存"></a>4.将公式写入单元格并保存</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sheet[&#39;K11&#39;] &#x3D; &#39;&#x3D;AVERAGE(K1:K10)&#39; </span><br></pre></td></tr></table></figure>

<h3 id="5-插入一行或多行"><a href="#5-插入一行或多行" class="headerlink" title="5.插入一行或多行"></a>5.插入一行或多行</h3><p><img src="http://iubest.gitee.io/pic/640-1601168112427.png" alt="img"></p>
<h3 id="6-插入一列或多列"><a href="#6-插入一列或多列" class="headerlink" title="6. 插入一列或多列"></a>6. 插入一列或多列</h3><p><img src="http://iubest.gitee.io/pic/640-1601168112426.png" alt="img"></p>
<h3 id="7-删除多行"><a href="#7-删除多行" class="headerlink" title="7.删除多行"></a>7.删除多行</h3><p><img src="http://iubest.gitee.io/pic/640-1601168112423.webp" alt="img"></p>
<h3 id="8-删除多列"><a href="#8-删除多列" class="headerlink" title="8.删除多列"></a>8.删除多列</h3><p><img src="http://iubest.gitee.io/pic/640-1601168112425.png" alt="img"></p>
<h3 id="9-移动范围数据"><a href="#9-移动范围数据" class="headerlink" title="9. 移动范围数据"></a>9. 移动范围数据</h3><p><img src="http://iubest.gitee.io/pic/640-1601168112421.png" alt="img"></p>
<h3 id="10-创建新的Excel表格"><a href="#10-创建新的Excel表格" class="headerlink" title="10. 创建新的Excel表格"></a>10. 创建新的Excel表格</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">from openpyxl import Workbook</span><br><span class="line">workbook &#x3D; Workbook()</span><br></pre></td></tr></table></figure>

<h2 id="Excel样式调整"><a href="#Excel样式调整" class="headerlink" title="Excel样式调整"></a>Excel样式调整</h2><h3 id="1-设置字体样式"><a href="#1-设置字体样式" class="headerlink" title="1. 设置字体样式"></a>1. 设置字体样式</h3><p><img src="http://iubest.gitee.io/pic/640-1601168167823.png" alt="img"></p>
<h3 id="2-设置对齐样式"><a href="#2-设置对齐样式" class="headerlink" title="2. 设置对齐样式"></a>2. 设置对齐样式</h3><p><img src="http://iubest.gitee.io/pic/640-1601168112423.png" alt="img"></p>
<ul>
<li>水平对齐:<code>distributed, justify, center, left, fill, centerContinuous, right, general</code></li>
<li>垂直对齐:<code>bottom, distributed, justify, center, top</code></li>
</ul>
<h3 id="3-设置边框样式"><a href="#3-设置边框样式" class="headerlink" title="3. 设置边框样式"></a>3. 设置边框样式</h3><p><img src="http://iubest.gitee.io/pic/640-1601168112424.png" alt="img"></p>
<ul>
<li>边线样式:<code>double, mediumDashDotDot, slantDashDot, dashDotDot, dotted, hair, mediumDashed, dashed, dashDot, thin, mediumDashDot, medium, thick</code></li>
</ul>
<h3 id="4-设置单元格填充样式"><a href="#4-设置单元格填充样式" class="headerlink" title="4. 设置单元格填充样式"></a>4. 设置单元格填充样式</h3><p><img src="http://iubest.gitee.io/pic/640-1601168112422.png" alt="img"></p>
<h3 id="5-设置行高和列宽"><a href="#5-设置行高和列宽" class="headerlink" title="5. 设置行高和列宽"></a>5. 设置行高和列宽</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sheet.row_dimensions[1].height &#x3D; 50 </span><br><span class="line">sheet.column_dimensions[&#39;C&#39;].width &#x3D; 20 </span><br></pre></td></tr></table></figure>

<h3 id="6-单元格合并与取消"><a href="#6-单元格合并与取消" class="headerlink" title="6. 单元格合并与取消"></a>6. 单元格合并与取消</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"># 合并</span><br><span class="line">sheet.merge_cells(&#39;A1:B2&#39;) </span><br><span class="line">sheet.merge_cells(start_row&#x3D;1, start_column&#x3D;3, </span><br><span class="line">                  end_row&#x3D;2, end_column&#x3D;4)</span><br><span class="line"></span><br><span class="line"># 取消合并</span><br><span class="line">sheet.unmerge_cells(&#39;A1:B2&#39;) </span><br><span class="line">sheet.unmerge_cells(start_row&#x3D;1, start_column&#x3D;3, </span><br><span class="line">                    end_row&#x3D;2, end_column&#x3D;4)</span><br></pre></td></tr></table></figure>

<hr>
<p>有了上面的基础知识之后我们就能更快速的与办公自动化需求结合使用，附几篇往期涉及openpyxl自动化相关文章大家体会一下👇(点击查看)</p>
<p><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s?__biz=MzI1MTUyMjc1Mg==&mid=2247497825&idx=1&sn=7cc7da0ffe1ad480881c5ac25d7f545e&scene=21#wechat_redirect">1. Python+Excel+Word一秒制作百份合同</a></p>
<p><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s?__biz=MzI1MTUyMjc1Mg==&mid=2247497058&idx=1&sn=e96efd9a264e5e9e420016597678589b&scene=21#wechat_redirect">2. 老板让我从几百个Excel中查找数据，我用Python一分钟搞定！</a></p>
<p><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s?__biz=MzI1MTUyMjc1Mg==&mid=2247495588&idx=1&sn=66051926b3465ba0ab874377a1fd13bc&scene=21#wechat_redirect">3. Python办公自动化｜批量提取Excel数据</a></p>
 
      <!-- reward -->
      
      <div id="reword-out">
        <div id="reward-btn">
          打赏
        </div>
      </div>
      
    </div>
    

    <!-- copyright -->
    
    <div class="declare">
      <ul class="post-copyright">
        <li>
          <i class="ri-copyright-line"></i>
          <strong>版权声明： </strong>
          
          本博客所有文章除特别声明外，著作权归作者所有。转载请注明出处！
          
        </li>
      </ul>
    </div>
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=http://example.com/2020/11/11/oa/%E8%AF%A6%E8%A7%A3Python%E6%93%8D%E4%BD%9CExcel%E7%A5%9E%E5%99%A8openpyxl%E7%9A%84%E5%90%84%E7%A7%8D%E6%93%8D%E4%BD%9C/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/oa/" rel="tag">oa</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2020/11/11/oa/Python%E5%AE%9E%E7%8E%B0%E6%95%B0%E6%8D%AE%E5%86%99%E5%85%A5%20Excel%20%E7%9A%84%E4%B8%89%E7%A7%8D%E6%A8%A1%E5%9D%97/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            Python实现数据写入 Excel 的三种模块.md
          
        </div>
      </a>
    
    
      <a href="/2020/11/11/deploy/Centos7%E5%8D%87%E7%BA%A7sqlite3/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">Centos7升级sqlite3.md</div>
      </a>
    
  </nav>

   
<!-- valine评论 -->
<div id="vcomments-box">
  <div id="vcomments"></div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js"></script>
<script>
  new Valine({
    el: "#vcomments",
    app_id: "",
    app_key: "",
    path: window.location.pathname,
    avatar: "monsterid",
    placeholder: "给我的文章加点评论吧~",
    recordIP: true,
  });
  const infoEle = document.querySelector("#vcomments .info");
  if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
    infoEle.childNodes.forEach(function (item) {
      item.parentNode.removeChild(item);
    });
  }
</script>
<style>
  #vcomments-box {
    padding: 5px 30px;
  }

  @media screen and (max-width: 800px) {
    #vcomments-box {
      padding: 5px 0px;
    }
  }

  #vcomments-box #vcomments {
    background-color: #fff;
  }

  .v .vlist .vcard .vh {
    padding-right: 20px;
  }

  .v .vlist .vcard {
    padding-left: 10px;
  }
</style>

 
     
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2015-2020
        <i class="ri-heart-fill heart_icon"></i> TzWind
      </li>
    </ul>
    <ul>
      <li>
        
        
        
        由 <a href="https://hexo.io" target="_blank">Hexo</a> 强力驱动
        <span class="division">|</span>
        主题 - <a href="https://github.com/Shen-Yu/hexo-theme-ayer" target="_blank">Ayer</a>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>访问人数:<span id="busuanzi_value_site_uv"></span></s>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>浏览次数:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
      </li>
    </ul>
  </div>
</footer>
      <div class="float_btns">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

      </div>
    </main>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="Hexo"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" target="_blank" rel="noopener" href="http://www.baidu.com">百度</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/friends">友链</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/2019/about">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="搜索">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <script>
      if (window.matchMedia("(max-width: 768px)").matches) {
        document.querySelector('.content').classList.remove('on');
        document.querySelector('.sidebar').classList.remove('on');
      }
    </script>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    
  </div>
</div>
    
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->


<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto'
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>

<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->


<link rel="stylesheet" href="/css/clipboard.css">

<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>


<!-- CanvasBackground -->


    
  </div>
</body>

</html>